/**********************************************************************************
* CLASS
*		RWxml
*
* DESCRIPTION
* 		Read and Write xml file
*
* REVISION HISTORY
*		Revision 1.0 2008/12/10 Atthaboon S.
*		First Release
*
**********************************************************************************/
var RWXml = {

	/****************************************************
	* FUNCTION 
	*		loadXML
	*
	* DESCRIPTION
	* 		Create Dom Book Page.
	*
	* ARGUMENTS
	*		bookXML		:	String of xml Book Page.
	*		pageGenesis	:	Page Genesis Object
	*
	****************************************************/
	loadXML : function(bookXML,pageGenesis)
	{
		//Load XML string into DOM Object
		try //Internet Explorer
	    {
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(bookXML);
		}
		catch(e)
		{
			try //Firefox, Mozilla, Opera, etc.
			{
				parser=new DOMParser();
				xmlDoc=parser.parseFromString(bookXML,"text/xml");
			}
			catch(e) {alert("Brower not support DOM Object")}
		}
		
		try 
		{
			// Get the root node (BookPage)
			var domPage = xmlDoc.getElementsByTagName('bookpage')[0];
			for (var i =0; i<domPage.childNodes.length; i++) 
			{
				var node = domPage.childNodes[i];
				if('bookimage' == node.nodeName)
				{
					var genesisObj = this.loadBookImage(node);
					pageGenesis[pageGenesis.length] = genesisObj; 
					pageGenesis[0].getDomObj().appendChild(genesisObj.getDomObj());
				}
				else if('content' == node.nodeName)
				{
					var genesisObj = this.loadBookContent(node);
					pageGenesis[pageGenesis.length] = genesisObj; 
					pageGenesis[0].getDomObj().appendChild(genesisObj.getDomObj());
				}
				else if('externalembeded' == node.nodeName)
				{
					var genesisObj = this.loadBookExternalEmbeded(node);
					pageGenesis[pageGenesis.length] = genesisObj; 
					pageGenesis[0].getDomObj().appendChild(genesisObj.getDomObj());
				}
				else if('genesis' == node.nodeName)
				{
					var genesisObj = this.loadBookGenesis(node,pageGenesis);
					pageGenesis[pageGenesis.length] = genesisObj;
					pageGenesis[0].getDomObj().appendChild(genesisObj.getDomObj());
				}
			}
		}
		catch(e) 
		{
		}
	},
	
	/****************************************************
	* FUNCTION 
	*		loadBookImage
	*
	* DESCRIPTION
	* 		Create Book Image from xml Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookImage Dom
	*
	* RETURN
	*		BookImageObject
	*
	****************************************************/
	loadBookImage : function(bookDom)
	{
		var style		= bookDom.getElementsByTagName('propertystyle')[0];
		var bookimage  	= new BookImage(bookDom.getElementsByTagName('src')[0].getAttribute('value'),parseInt(style.getElementsByTagName('left')[0].getAttribute('value')),parseInt(style.getElementsByTagName('top')[0].getAttribute('value')));
		bookimage.setSize(parseInt(style.getElementsByTagName('width')[0].getAttribute('value')),parseInt(style.getElementsByTagName('height')[0].getAttribute('value')));
		bookimage.fade(parseInt(style.getElementsByTagName('opacity')[0].getAttribute('value')),parseInt(style.getElementsByTagName('opacity')[0].getAttribute('value')),0);
		return bookimage;
	},
	
	/****************************************************
	* FUNCTION 
	*		loadBookContent
	*
	* DESCRIPTION
	* 		Create Book Content from xml Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookImage Dom
	*
	* RETURN
	*		ContentObject
	*
	****************************************************/
	loadBookContent : function(bookDom)
	{
		var style		= bookDom.getElementsByTagName('propertystyle')[0];
		var content  	= new Content(parseInt(style.getElementsByTagName('left')[0].getAttribute('value')),parseInt(style.getElementsByTagName('top')[0].getAttribute('value')));
		content.setSize(parseInt(style.getElementsByTagName('width')[0].getAttribute('value')),parseInt(style.getElementsByTagName('height')[0].getAttribute('value')));
		// Remove tag comment CDATA
		var text = bookDom.getElementsByTagName('innerhtml')[0].firstChild.data;
		text = text.substring(7,text.length-2);
		content.setText(text);
		return content;
	},
	
	/****************************************************
	* FUNCTION 
	*		loadBookExternalEmbeded
	*
	* DESCRIPTION
	* 		Create Book External Embeded from xml Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookGenesis Dom
	*
	* RETURN
	*		externalEmbededObject
	*
	****************************************************/
	loadBookExternalEmbeded : function(bookDom)
	{
		var style 		    		= bookDom.getElementsByTagName('propertystyle')[0];
		var externalEmbededObject	= new ExternalEmbeded(parseInt(style.getElementsByTagName('left')[0].getAttribute('value')),parseInt(style.getElementsByTagName('top')[0].getAttribute('value')),bookDom.getElementsByTagName('innerhtml')[0].firstChild.data);
		externalEmbededObject.setSize(parseInt(style.getElementsByTagName('width')[0].getAttribute('value')),parseInt(style.getElementsByTagName('height')[0].getAttribute('value')));
		return externalEmbededObject;
	},
	
	/****************************************************
	* FUNCTION 
	*		loadBookGenesis
	*
	* DESCRIPTION
	* 		Create Book Genesis from xml Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookGenesis Dom
	*
	* RETURN
	*		GenesisObject
	*
	****************************************************/
	loadBookGenesis : function(bookDom,pageGenesis)
	{
		var style 		    = bookDom.getElementsByTagName('propertystyle')[0];
		var bookgenesis  	= new Genesis(parseInt(style.getElementsByTagName('left')[0].getAttribute('value')),parseInt(style.getElementsByTagName('top')[0].getAttribute('value')));
		bookgenesis.setSize(parseInt(style.getElementsByTagName('width')[0].getAttribute('value')),parseInt(style.getElementsByTagName('height')[0].getAttribute('value')));
		var childnode		= bookDom.childNodes;
		for (var i =0; i<childnode.length; i++) 
		{
			var node	= childnode[i];
			if('bookimage' == node.nodeName)
			{
				var bookimage = this.loadBookImage(node);
				bookgenesis.getDomObj().appendChild(bookimage.getDomObj());
				pageGenesis[pageGenesis.length] = bookimage;
			}
			else if('content' == node.nodeName)
			{
				var content   = this.loadBookContent(node);
				bookgenesis.getDomObj().appendChild(content.getDomObj());
				pageGenesis[pageGenesis.length] = content;
			}
			else if('externalembeded' == node.nodeName)
			{
				var externalEmbeded	=	this.loadBookExternalEmbeded(node);
				bookgenesis.getDomObj().appendChild(externalEmbeded.getDomObj());
				pageGenesis[pageGenesis.length] = externalEmbeded;
			}
		}
		return bookgenesis;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveXML
	*
	* DESCRIPTION
	* 		Create Dom Book Page.
	*
	* ARGUMENTS
	*		container		:	Genesis Object container
	*		index			:	Book Page index
	*
	****************************************************/
	saveXML : function(container,index)
	{
		// initial strXML string
		var strXML   = '<?xml version="1.0" encoding="WINDOWS-874"?>';
		var root	 = new Element('Root',{});
		var bookpage = new Element('BookPage',{'value':index});
		root.appendChild(bookpage);
		
		// Add xml form tempXML into strXML following dom hirachy
		// 1. Add Container
		var tempGenesis = new Array();
		var tempGenesisXML = new Array();
		for (var i = 1;i<container.length;i++)
		{
			var genesisObj = container[i];
			if("Genesis" == genesisObj.getDomType())
			{
				tempGenesis[tempGenesis.length] = genesisObj;
				tempGenesisXML[tempGenesisXML.length] = this.saveBookGenesis(genesisObj);
				//bookpage.appendChild(tempXML[i-1]);
			}
		}
		
		// 2. Add child inside Container and normal child
		for (var i = 1;i<container.length;i++)
		{
			var genesisObj = container[i];
			if("Genesis" != genesisObj.getDomType())
			{
				var inContainer = false;
				//Verify inside container
				for(var j=0;j<tempGenesis.length;j++)
				{

					if(tempGenesis[j].getDomObj() == genesisObj.getDomObj().parentNode)
					{	
						inContainer = true;
						if("BookImage" == genesisObj.getDomType())
						{
							var tempXML = tempGenesisXML[j]
							tempXML.appendChild(this.saveBookImage(genesisObj));
							tempGenesisXML[j] = tempXML;
							//tempGenesis[j].getDomObj().appendChild(this.saveBookImage(genesisObj));
						}
						else if("Content" == genesisObj.getDomType())
						{
							var tempXML = tempGenesisXML[j]
							tempXML.appendChild(this.saveBookContent(genesisObj));
							tempGenesisXML[j] = tempXML;
							//tempGenesis[j].getDomObj().appendChild(this.saveBookContent(genesisObj));
						}
						else if("ExternalEmbeded" == genesisObj.getDomType())
						{
							var tempXML = tempGenesisXML[j]
							tempXML.appendChild(this.saveBookExternalEmbeded(genesisObj));
							tempGenesisXML[j] = tempXML;
							//tempGenesis[j].getDomObj().appendChild(this.saveBookExternalEmbeded(genesisObj));
						}
						break;
					}
				}
				if(!inContainer)
				{
					if("BookImage" == genesisObj.getDomType())
					{
						bookpage.appendChild(this.saveBookImage(genesisObj));
					}
					else if("Content" == genesisObj.getDomType())
					{
						bookpage.appendChild(this.saveBookContent(genesisObj));
					}
					else if('ExternalEmbeded' == genesisObj.getDomType())
					{
						bookpage.appendChild(this.saveBookExternalEmbeded(genesisObj));
					}
				}
			}
		}
		
		// 3. Add Gensis object
		for(var j=0;j<tempGenesis.length;j++)
		{
			bookpage.appendChild(tempGenesisXML[j]);
		}
		
		var strXML = strXML+''+root.innerHTML;
		// Replace node name for support ie
		strXML = strXML.replace(/<:/g, "<");
		strXML = strXML.replace(RegExp("</:","g"), "</");
		return strXML;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveBookImage
	*
	* DESCRIPTION
	* 		Create String Book Image from Dom.
	*
	* ARGUMENTS
	*		bookDom	:	BookImage Dom
	*
	* RETURN
	*		bookxmlDom
	*
	****************************************************/
	saveBookImage : function(genesis)
	{
		var bookDom = genesis.getDomObj();
		var genesisObj = new Element("BookImage",{});
		var src		   = new Element("src",{'value':bookDom.src});
		genesisObj.appendChild(src);
		var style      = new Element('PropertyStyle',{});
		genesisObj.appendChild(style);
		style.appendChild(new Element("left"   ,{'value':bookDom.style.left}));
		style.appendChild(new Element("top"    ,{'value':bookDom.style.top}));
		style.appendChild(new Element("width"  ,{'value':bookDom.style.width}));
		style.appendChild(new Element("height" ,{'value':bookDom.style.height}));
		style.appendChild(new Element("opacity",{'value':bookDom.getStyle('opacity')}));
		return genesisObj;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveBookContent
	*
	* DESCRIPTION
	* 		Create String Book Content from Dom.
	*
	* ARGUMENTS
	*		bookDom	:	Content Dom
	*
	* RETURN
	*		bookxmlDom
	*
	****************************************************/
	saveBookContent : function(genesis)
	{
		var bookDom = genesis.getDomObj();
		var genesisObj = new Element('Content',{});
		var style      = new Element('PropertyStyle',{});
		genesisObj.appendChild(style);
		style.appendChild(new Element("left"    ,{'value':bookDom.style.left}));
		style.appendChild(new Element("top"     ,{'value':bookDom.style.top}));
		style.appendChild(new Element("width"   ,{'value':bookDom.style.width}));
		style.appendChild(new Element("height"  ,{'value':bookDom.style.height}));
		var innerhtml  = new Element("innerHTML",{'Type':'html'});
		genesisObj.appendChild(innerhtml);
		innerhtml.innerHTML = "<![CDATA["+bookDom.innerHTML+"]]>";
		return genesisObj;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveBookExternalEmbeded
	*
	* DESCRIPTION
	* 		Create String Book ExternalEmbeded from Dom.
	*
	* ARGUMENTS
	*		bookDom	:	ExternalEmbeded Dom
	*
	* RETURN
	*		bookxmlDom
	*
	****************************************************/
	saveBookExternalEmbeded : function(genesis)
	{
		var bookDom = genesis.getDomObj();
		var genesisObj = new Element('ExternalEmbeded',{});
		var style      = new Element('PropertyStyle',{});
		genesisObj.appendChild(style);
		style.appendChild(new Element("left"   ,{'value':bookDom.style.left}));
		style.appendChild(new Element("top"    ,{'value':bookDom.style.top}));
		style.appendChild(new Element("width"  ,{'value':bookDom.style.width}));
		style.appendChild(new Element("height" ,{'value':bookDom.style.height}));
		var innerhtml  = new Element('innerhtml',{'Type':'html'});
		genesisObj.appendChild(innerhtml);
		innerhtml.innerHTML = "<!--"+genesis.getCode()+"-->";
		return genesisObj;
	},
	
	/****************************************************
	* FUNCTION 
	*		saveBookGenesis
	*
	* DESCRIPTION
	* 		Create String Book Genesis from Dom.
	*
	* ARGUMENTS
	*		bookDom	:	Content Dom
	*
	* RETURN
	*		bookxmlDom
	*
	****************************************************/
	saveBookGenesis : function(genesis)
	{
		var bookDom = genesis.getDomObj();
		var genesisObj = new Element('Genesis',{});
		var style      = new Element('PropertyStyle',{});
		genesisObj.appendChild(style);
		style.appendChild(new Element("left"   ,{'value':bookDom.style.left}));
		style.appendChild(new Element("top"    ,{'value':bookDom.style.top}));
		style.appendChild(new Element("width"  ,{'value':bookDom.style.width}));
		style.appendChild(new Element("height" ,{'value':bookDom.style.height}));
		return genesisObj;
	}
};